<?php

namespace App\Admin\Controllers;

use App\Models\Card;
use App\Models\CardUser;
use App\Models\Station;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Grid\Filter;

class CardUserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '小美卡-用户';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new CardUser);
        $grid->model()->with(['user'])->orderBy("id", "desc");

        $grid->column('id', __('Id'));
        $grid->column('name', '名称');
        $grid->column('user.name', '用户昵称');
        $grid->column('user.id', '用户ID');
        $grid->column('price', '价格');
        $grid->column('number', '次数')->display(function () {
            return $this->numberHuman;
        });
        $grid->column('surplus_number', '剩余次数')->display(function () {
            return $this->surplusNumberHuman;
        });
        $grid->column('expiration_time', '过期时间');
        $grid->column('validity_day', '有效期')->display(function () {
            return $this->validityDayHuman;
        });

        $grid->column('created_time', '创建时间');

        $grid->column('status', '状态')->display(function () {
            return $this->statusText;
        })->label([CardUser::STATUS_VALID => 'success', CardUser::STATUS_INVALID => 'danger']);

        $grid->disableCreateButton();
        $grid->disableRowSelector();
        $grid->actions(function ($actions) {
            $actions->disableEdit();
            $actions->disableDelete();
        });

        $grid->filter(function (Filter $filters) {
            $filters->equal('user_id', '用户')->select(function ($id) {
                $user = User::find($id);
                if ($user) {
                    return [$user->id => sprintf("%s【%s】", $user->name, $user->id)];
                }
            })->ajax(route('user.json.select'));
            $filters->equal('card_id', '来源卡')->select(Card::pluck("name", "id"));
            $filters->equal('status', '状态')->select(CardUser::$statusText);
            $filters->between('created_time', '时间范围')->datetime();
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(CardUser::with('user')->findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', '名称');
        $show->field('user_id', '用户')->as(function () {
            return sprintf("%s(%s)", optional($this->user)->name, optional($this->user)->id);
        });
        $show->field('price', '价格');
        $show->field('status', '状态')->as(function () {
            return $this->statusText;
        });
        $show->field('card_id', '来源卡ID');
        $show->field('number', '次数')->as(function () {
            return $this->numberHuman;
        });
        $show->field('surplus_number', '剩余次数')->as(function () {
            return $this->surplusNumberHuman;
        });
        $show->field('expiration_time', '过期时间');
        $show->field('validity_day', '有效期')->as(function () {
            return $this->validityDayHuman;
        });
        $show->field('image_url', '图片')->image();
        $show->field('careful_item', '注意事项');
        $show->field('mcstation_ids', '适用门店')->as(function () {
            return Station::find($this->mcstation_ids)->implode("name", "，");
        });
        $show->field('created_time', '创建时间');
        $show->field('updated_time', '更新时间');

        $show->panel()->tools(function ($tools) {
            $tools->disableEdit();
            $tools->disableDelete();
        });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new CardUser);

        $form->text('name', __('Name'));
        $form->number('user_id', __('User id'));
        $form->number('price', __('Price'));
        $form->number('status', __('Status'))->default(1);
        $form->number('card_id', __('Card id'));
        $form->number('number', __('Number'));
        $form->number('surplus_number', __('Surplus number'));
        $form->datetime('expiration_time', __('Expiration time'))->default(date('Y-m-d H:i:s'));
        $form->number('validity_day', __('Validity day'));
        $form->text('image_url', __('Image url'));
        $form->textarea('careful_item', __('Careful item'));
        $form->textarea('mcstation_ids', __('Mcstation ids'));

        return $form;
    }
}
